<?php

namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\Authorizable;

use Illuminate\Http\Request;
use App\Post;
use App\Comment;
use App\Zan;
class PostsController extends Controller
{
	//文章列表页
    public function index(){

        /*
    	$app = app(); //app()是容器
        $log = $app->make('log');//绑定了log这个别名
        $log->info('index_post+ww',['success'=>'yes']);
        */
       // \Log::info('post:)',['success'=>'yes']);
        $user = \Auth::user();
    	// $posts = Post::orderBy('created_at','desc')->withCount("comments")->paginate(6);
        $posts = Post::orderBy('created_at','desc')->withCount(["comments","zans"])->paginate(6);
    	return view('posts/index',compact('posts'));
    }
    //文章详情页
    public function show(\App\Post $post){
        $post->load('comments');//为符合mvc模式，数据，不在view层去获取，而在这里获取。
    	return view('posts/show',compact('post'));
    }
    //创建文章逻辑
    public function create(){
    	$wangedit = true;
    	return view('posts/create',compact('wangedit'));
    }
    //修改文章逻辑
    public function edit(Post $post){
    	$wangedit = true;
    	return view('posts/edit',compact('post','wangedit'));
    }

    public function update(Post $post){
    	//验证
    	$this->validate(request(),[
    		'title' => 'required|string|max:100|min:5',
    		'content' => 'required|string|min:10'
    	]);
        $this->authorize('update', $post);
    	//逻辑
    	$post->title = request('title');
    	$post->content = request('content');
    	$post->save();
        //$post->update(request(['title', 'content']));
    	//渲染
    	return redirect("./posts/{$post->id}");
    }
    //存储文章逻辑
    public function store(){
    	// $post = new Post();
    	// $post->title = request('title');
    	// $post->content = request('content');
    	// $post->save();
    	// $this->validate(request(),[
    	// 	'title' => 'required|string|max:100|min:5',
    	// 	'content' => 'required|string|min:10'
    	// ],[
    	// 	'title.min'=> '文字标题过短不得少于5个字符'

    	// ]);
    	//验证
    	$this->validate(request(),[
    		'title' => 'required|string|max:100|min:5',
    		'content' => 'required|string|min:10'
    	]);
    	//逻辑
    	$params = request(['title','content']);
    	$params['user_id'] = \Auth::id();
    	// dd($params);
    	Post::Create($params);
    	// dd(\Request::all());
    	// dd(request());
    	//渲染
    	return redirect("./posts");
    }


    //删除文章逻辑
    public function delete(Post $post){
    	//TODO：用户的权限验证
        $this->authorize('delete',$post);
    	$post->delete();
    	return redirect("./posts");
    }

    //上传图片
    public function imageUpload(Request $request){
    	// dd(request()->all());
    	$path = $request->file('uploadImage')->storePublicly(md5(time()));
    	$url = array();
    	$url[] = asset('storage/'.$path);
    	$data = array('errno'=>0,'data'=>$url);
    	return json_encode($data);
    }

    //创建评论
    public function comment(Post $post){
        //验证
        $this->validate(request(),[
            'content' => 'required|string|min:1'
        ]);
        //逻辑
        /*
        方法一：
        $content = request('content');
        $user_id = \Auth::id();
        $post_id = $post->id;
        $params = compact('content','user_id','post_id');
        Comment::create($params);*/
        //方法二：
        $comment = new Comment();
        $comment->user_id = \Auth::id();
        $comment->post_id = $post->id;
        $comment->content = request('content');
        $post->comments()->save($comment);
        //渲染
        return back();
    }

    //赞
    public function zan(Post $post){
        $param = [
            'user_id'=> \Auth::id(),
            'post_id'=>$post->id,
        ];
        Zan::firstOrCreate($param); //存在就查找，不存在就新增
        return back();
    }

    //取消赞
    public function unzan(Post $post){
        $post->zan(\Auth::id())->delete();
        return back();
    }
}
